package com.hfjyz.judge.judge.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.base.util.BaseUtil;
import com.hfjyz.judge.judge.bean.JudgeBean;
import com.hfjyz.judge.judge.bean.JudgeExcelBean;
import com.hfjyz.judge.judge.bean.JudgeSearchBean;
import com.hfjyz.judge.judge.service.JudgeService;
import com.hfjyz.judge.judgecall.bean.JudgeCallBean;
import com.hfjyz.judge.judgecall.service.JudgeCallService;

public class JudgeAutoSelectAction extends GridBaseAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 2363637918721745365L;

    private JudgeSearchBean judgeSearch = null;
    private Integer autoSelectCount = null;
    private JudgeCallBean judgeCall = null;
    private String judgeIds = null;

    private InputStream excelStream = null;
    private String downloadFileName = null;

    private JudgeService service = null;

    private JudgeCallService callService = null;

    public InputStream getExcelStream() {
        return excelStream;
    }

    public void setExcelStream(InputStream excelStream) {
        this.excelStream = excelStream;
    }

    public String getDownloadFileName() {
        return downloadFileName;
    }

    public void setDownloadFileName(String downloadFileName) {
        this.downloadFileName = downloadFileName;
    }

    public JudgeSearchBean getJudgeSearch() {
        return judgeSearch;
    }

    public void setJudgeSearch(JudgeSearchBean judgeSearch) {
        this.judgeSearch = judgeSearch;
    }

    public Integer getAutoSelectCount() {
        return autoSelectCount;
    }

    public void setAutoSelectCount(Integer autoSelectCount) {
        this.autoSelectCount = autoSelectCount;
    }

    public JudgeCallBean getJudgeCall() {
        return judgeCall;
    }

    public void setJudgeCall(JudgeCallBean judgeCall) {
        this.judgeCall = judgeCall;
    }

    public JudgeService getService() {
        return service;
    }

    public void setService(JudgeService service) {
        this.service = service;
    }

    public JudgeCallService getCallService() {
        return callService;
    }

    public void setCallService(JudgeCallService callService) {
        this.callService = callService;
    }

    public String getJudgeIds() {
        return judgeIds;
    }

    public void setJudgeIds(String judgeIds) {
        this.judgeIds = judgeIds;
    }

    @Override
    protected List<JudgeBean> getBeanList() throws BaseException {
        List<JudgeBean> judgeList = service.findJudgeWithoutPage(getSearchCondition(), judgeSearch);
        if (autoSelectCount == null) {
            return judgeList;
        }
        Set<Integer> indexSet = BaseUtil.getRandomIndex(judgeList.size(), autoSelectCount);
        List<JudgeBean> retList = new ArrayList<JudgeBean>();
        Iterator<Integer> it = indexSet.iterator();
        while (it.hasNext()) {
            retList.add(judgeList.get(it.next()));
        }
        return retList;
    }

    @Override
    protected long getTotalCount() throws BaseException {
        long count = service.getJudgeCount(judgeSearch);
        if (autoSelectCount == null) {
            return count;
        }
        if (count < autoSelectCount) {
            return count;
        } else {
            return autoSelectCount;
        }
    }

    public String export() throws BaseException {
        ByteArrayOutputStream baos = null;
        try {
            String rootPath = ServletActionContext.getServletContext().getRealPath("/");
            String templatePathSingle = rootPath + "WEB-INF\\conf\\judge\\"
                    + JudgeExcelAction.XLS_JUDGE_TEMPLATE;
            String templatePathMulti = rootPath + "WEB-INF\\conf\\judge\\"
                    + JudgeExcelAction.XLS_JUDGE_LIST_TEMPLATE;
            List<JudgeBean> list = getBeanList();
            List<JudgeExcelBean> excelList = getExcelBeanList(list);

            Workbook wb = null;
            if (excelList.size() == 1) {
                wb = getService().exportJudge(templatePathSingle, excelList);
            } else {
                wb = getService().exportJudge(templatePathMulti, excelList);
            }
            baos = new ByteArrayOutputStream();
            wb.write(baos);
            baos.flush();
            byte[] excelBytes = baos.toByteArray();
            excelStream = new ByteArrayInputStream(excelBytes, 0, excelBytes.length);

            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            downloadFileName = "评委信息详细_" + sf.format(new Date()) + ".xls";
            downloadFileName = new String(downloadFileName.getBytes(), "ISO8859-1");
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException("导出Excel失败！", e);
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
            }
        }
        return SUCCESS;
    }

    public String saveAutoSelectJudgeCall() {
        map = new HashMap<String, Object>();
        try {
            if (judgeIds == null || judgeIds.isEmpty()) {
                throw new BaseException("错误：没有选择评委！");
            }
            String[] targetIds = judgeIds.split(",");
            List<JudgeCallBean> callBeanList = new ArrayList<JudgeCallBean>();
            for (String id : targetIds) {
                Integer targetId = Integer.valueOf(id);
                JudgeCallBean call = new JudgeCallBean();
                JudgeBean judge = new JudgeBean();
                judge.setId(targetId);
                call.setTarget(judge);
                call.setCallName(judgeCall.getCallName());
                call.setCallReason(judgeCall.getCallReason());
                call.setCallRemark(judgeCall.getCallRemark());
                call.setCallAim(judgeCall.getCallAim());

                callBeanList.add(call);
            }

            callService.saveJudgeCall(callBeanList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    private List<JudgeExcelBean> getExcelBeanList(List<JudgeBean> judgeList) {
        List<JudgeExcelBean> beanList = new ArrayList<JudgeExcelBean>();
        if (judgeList == null || judgeList.isEmpty()) {
            return beanList;
        }

        for (JudgeBean judge : judgeList) {
            beanList.add(new JudgeExcelBean(judge));
        }
        return beanList;
    }
}


